package br.com.foxline.atlasmobile.adapter;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.lang.ref.WeakReference;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
import br.com.foxline.atlasmobile.R;
import br.com.foxline.atlasmobile.Util;

public class ImageLoadTask extends AsyncTask<Uri, Void, Bitmap> {
	private final WeakReference<ImageView> imageViewReference;
	private Uri data;
	private ImageView imageView;
	private Integer width;
	private Integer height;
	private boolean full;
	private ProgressDialog dialog;
	private String base64String;

	public ImageLoadTask(ImageView imageView) {
		imageViewReference = new WeakReference<ImageView>(imageView);
		this.imageView = imageView;
	}

	public ImageLoadTask(ImageView imageView, Integer width, Integer height,
			boolean full) {
		imageViewReference = new WeakReference<ImageView>(imageView);
		this.imageView = imageView;
		this.width = width == null ? 200 : width;
		this.height = height == null ? 200 : height;
		this.full = full;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (!full) {
			imageView.setImageBitmap(Util.decodeSampledBitmapFromResource(
					imageView.getContext().getResources(),
					R.drawable.empty_photo, this.width, this.height));
		} else {
			dialog = ProgressDialog.show(imageView.getContext(), "",
					"Caregando Imagem...", true);
		}
	}

	@Override
	protected Bitmap doInBackground(Uri... params) {
		data = params[0];
		Bitmap decodeSampledBitmapFromResource = Util
				.decodeSampledBitmapFromResource(data, this.width, this.height);

		if (full) {
			base64String = encodeImg(decodeSampledBitmapFromResource);
		}
		return decodeSampledBitmapFromResource;
	}

	@Override
	protected void onPostExecute(Bitmap bitmap) {
		if (imageViewReference != null && bitmap != null) {
			final ImageView imageView = imageViewReference.get();
			if (imageView != null) {
				imageView.setImageBitmap(bitmap);
			}
		}
		if (full) {
			dialog.dismiss();
//			WriteFile(base64String);
//			base64String = ReadFile();
//			imageView.setImageBitmap(decodeImg(bitmap));
		}
	}


	private String encodeImg(Bitmap bitmap) {
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
		byte[] b = bytes.toByteArray();
		return Base64.encodeToString(b, Base64.DEFAULT);
	}

	private Bitmap decodeImg(String bitmap) {
		byte[] decodeString = Base64.decode(bitmap, 0);
		Bitmap decodeByte = BitmapFactory.decodeByteArray(decodeString, 0,
				decodeString.length);
		return decodeByte;
	}

	public File getAlbumStorageDir() {
		File file = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
						+ "");

		return file;
	}

	public boolean WriteFile(String text) {
		try {
			File file = new File(getAlbumStorageDir(), "imagem.txt");
			FileOutputStream out = new FileOutputStream(file, true);
			out.write(text.getBytes());
			out.write("\n".getBytes());
			out.flush();
			out.close();
			return true;

		} catch (Exception e) {
			Log.e("ERROR", e.toString());
			return false;
		}
	}

	public String ReadFile() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(getAlbumStorageDir(), "imagem.txt")));
			String content = "";
			StringBuffer strAux = new StringBuffer();
			while ((content = br.readLine()) != null) {
				System.err.println("Lendo...");
					strAux.append(content);
			}
			return ""+strAux;

		} catch (Exception e) {
			Log.e("ERROR", e.toString());
			return "";
		}
	}
}
